<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { getPermissionListAPIPI, addPermissionAPI, updatePermissionAPI, deletePermissionAPI } from '@api/acl/menu/index.ts'
import type {
  permissionListType,
  permissionItemType,
  permissionParams,
} from '@api/acl/menu/type.ts'
import { ElMessage } from 'element-plus'

const loading = ref<boolean>(false)
const data = ref<permissionItemType[]>([])
// 获取菜单列表数据
const initFn = async () => {
  loading.value = true
  const res: permissionListType = await getPermissionListAPIPI()
  data.value = res.data
  loading.value = false
}

onMounted(() => initFn())

const dialogVisible = ref<boolean>(false)
const dialogTitle = ref<string>('')
const dialogParams = ref<permissionItemType>({})
// 点击添加菜单按钮
const handleAddFn = (row: permissionItemType) => {
  form.value = {}
  dialogVisible.value = true
  dialogTitle.value = '添加菜单'
  dialogParams.value = JSON.parse(JSON.stringify(row))
  form.value.level = (row.level as number) + 1
  form.value.pid = (row.id as number)
}
// 点击编辑菜单按钮
const handleEditFn = (row: permissionItemType) => {
  dialogVisible.value = true
  dialogTitle.value = '编辑菜单'
  dialogParams.value = JSON.parse(JSON.stringify(row))
  form.value = {
    level: (row.level as number),
    pid: (row.pid as number),
    name: row.name,
    code: row.code,
    id: row.id
  }
}

const form = ref<permissionParams>({})
// 点击表单确认按钮
const handleConfirmFn = () => {
  if(dialogTitle.value === '添加菜单') {
    addPermissionAPI(form.value).then(res => {
      if(res.code === 200) {
        ElMessage.success('添加成功')
        initFn()
        dialogVisible.value = false
      }
    })
  } else {
    updatePermissionAPI(form.value).then(res => {
      if(res.code === 200) {
        ElMessage.success('编辑成功')
        initFn()
        dialogVisible.value = false
      }
    })
  }
}


// 点击删除菜单按钮
const handleRemoveFn = (row: permissionItemType) => {
  console.log(row);
  deletePermissionAPI(row.id as number).then(res => {
      if(res.code === 200) {
        ElMessage.success('删除成功')
        initFn()
      }
    })
}
</script>

<template>
  <div>
    <el-table v-loading="loading" :data="data" border row-key="id">
      <el-table-column label="名称" prop="name"></el-table-column>
      <el-table-column label="权限值" prop="code"></el-table-column>
      <el-table-column label="修改时间" prop="updateTime"></el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button
            v-if="row.level !== 4"
            type="primary"
            size="small"
            @click="handleAddFn(row)"
          >
            {{ row.level === 3 ? '添加功能' : '添加菜单' }}
          </el-button>
          <el-button
            v-if="row.level !== 1"
            type="primary"
            size="small"
            @click="handleEditFn(row)"
          >
            编辑
          </el-button>
          <el-popconfirm
            title="是否确认删除该权限"
            @confirm="handleRemoveFn(row)"
          >
            <template #reference>
              <el-button
                v-if="row.level !== 1"
                type="danger"
                size="small"
              >
                删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加、编辑菜单弹窗 -->
    <el-dialog
      v-model="dialogVisible"
      title="Tips"
      width="30%"
    >
      <!-- 表单元素 -->
      <el-form :model="form">
      <el-form-item label="名称">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="权限">
        <el-input v-model="form.code" />
      </el-form-item>
    </el-form>

      <!-- 底部按钮 -->
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleConfirmFn">
            确认
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>
